package io.geekidea.springbootplus.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.annotations.CurrentUser;
import io.geekidea.springbootplus.system.entity.Address;
import io.geekidea.springbootplus.system.entity.CurrentUserInfo;
import io.geekidea.springbootplus.system.entity.Customer;
import io.geekidea.springbootplus.system.param.AddressPageParam;
import io.geekidea.springbootplus.system.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *  控制器
 *
 * @author miko
 * @since 2020-10-11
 */
@Slf4j
@RestController
@RequestMapping("/address")
@Module("system")
@Api(value = "API", tags = {""})
public class AddressControllerExtra extends BaseController {

    @Autowired
    private AddressService addressService;

    @PostMapping("/getAddressByUser")
    @ApiOperation(value = "用户地址", response = Address.class)
    public ApiResult<Address> getAddressByUser(@CurrentUser CurrentUserInfo customer) throws Exception {
        int schoolID = Integer.parseInt(customer.getSchoolID());
        int userid = customer.getCustomerID();
        QueryWrapper<Address> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("CustomerID", userid).eq("SchoolID", schoolID);
        Address address = addressService.getOne(queryWrapper);
        return ApiResult.ok(address);
    }
    @PostMapping("/updateAddress")
    @ApiOperation(value = "更新用户地址", response = Address.class)
    public ApiResult<Address> upsertAddress(@CurrentUser CurrentUserInfo customer,@RequestBody Address address) throws Exception {

        int schoolID = Integer.parseInt(customer.getSchoolID());
        address.setSchoolID(schoolID);

        int userid = customer.getCustomerID();
        address.setCustomerID(userid);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("CustomerID",userid);
        wrapper.eq("SchoolID",schoolID);
        Address ad = addressService.getOne(wrapper);
        if(ad==null){
            addressService.saveAddress(address);
        }else{
            address.setAddressID(ad.getAddressID());
            address.setCreateTime(ad.getCreateTime());
            addressService.updateAddress(address);
        }
        return ApiResult.ok(address);
    }


}

